﻿#include "FITKAbaRefPoint.h"
#include "FITK_Kernel/FITKCore/FITKDataRepo.h" 
#include "FITK_Kernel/FITKCore/FITKVec3D.h" 
#include "FITK_Interface/FITKInterfaceModel/FITKModelSet.h"
#include "FITK_Interface/FITKInterfaceModel/FITKNodeList.h"
#include "FITK_Interface/FITKInterfaceModel/FITKComponentManager.h"
#include "FITK_Interface/FITKInterfaceModel/FITKAssInstance.h"
#include "FITKAbaAssembly.h"

namespace Interface
{

    FITKAbaRefPoint::FITKAbaRefPoint()
    {
        this->enable(false);
    }

    void FITKAbaRefPoint::setComponentID(int id)
    {
        _setID = id;
    }


    int FITKAbaRefPoint::getComponentID()
    {
        return _setID;
    }

    FITKModelSet* FITKAbaRefPoint::getSet()
    {
         return FITKDATAREPO->getTDataByID<FITKModelSet>(_setID);
    }

    void FITKAbaRefPoint::setComponentModelID(const int modelID)
    {
        _modelID = modelID;
    }

    int FITKAbaRefPoint::getComponentModelID()
    {
        if (_modelID < 0)
        {
            //没有设置或者错误情况，默认返回集合的模型
            FITKModelSet* set = FITKDATAREPO->getTDataByID<FITKModelSet>(_setID);
            if (set) return _modelID;
            return set->getAbsModelID();
        }

        return _modelID;
    }

    bool FITKAbaRefPoint::getCoor(double* coor)
    {
        //判断自身装配
        FITKAbaAssembly* ass = this->getAbstractModelAs<FITKAbaAssembly>();
        if (ass == nullptr) return false;
        
        FITKAssemblyInstance* ins = FITKDATAREPO->getTDataByID<FITKAssemblyInstance>(_modelID);
        FITKAbaAssembly* assA = FITKDATAREPO->getTDataByID<FITKAbaAssembly>(_modelID);
        if (ins == nullptr && assA == nullptr) return false;
        //集合
        FITKModelSet* set = FITKDATAREPO->getTDataByID<FITKModelSet>(_setID);
        if (set == nullptr || set->getModelSetType() != FITKModelEnum::FMSNode) return false;
        const QList<int>&  mem = set->getMember();
        if (mem.isEmpty()) return false;
        const int id = mem.at(0);

        //装配的集合
        if (assA)
        {
            //节点id
            return ass->getPointCoor(id, coor);
        }
        //装配实例的集合
        else if(ins)
        {
            return ins->getPointCoor(id, coor);

        }
        return false;
    }

     

    int FITKAbaRefPoint::getNodeID()
    {
        //集合对象
        auto set = this->getSet();
        if (set == nullptr) return -1;
        //集合的节点成员
        return set->getFirstMemberID();
    }

//     void FITKAbaRefPoint::setDataObjectName(const QString & name)
//     {
//         FITKAbstractNDataObject::setDataObjectName(name);
//     }

    void FITKAbaRefPointManager::removeDataObj(Core::FITKAbstractDataObject* obj)
    {
        auto data = dynamic_cast<FITKAbaRefPoint*>(obj);
        this->removeSet(data);
        Core::FITKAbstractDataManager<FITKAbaRefPoint>::removeDataObj(obj);
    }

    void FITKAbaRefPointManager::removeDataByIndex(int index)
    {
        auto data = getDataByIndex(index);
        this->removeSet(data);
        Core::FITKAbstractDataManager<FITKAbaRefPoint>::removeDataObj(data);
    }

    void FITKAbaRefPointManager::removeDataByID(int id)
    {
        auto data = getDataByID(id);
        this->removeSet(data);
        Core::FITKAbstractDataManager<FITKAbaRefPoint>::removeDataObj(data);
    }
    
    void FITKAbaRefPointManager::removeDataByName(const QString &name, bool sense)
    {
        auto data = getDataByName(name,sense);
        this->removeSet(data);
        Core::FITKAbstractDataManager<FITKAbaRefPoint>::removeDataObj(data);
    }

    void FITKAbaRefPointManager::removeSet(FITKAbaRefPoint* ref)
    {
        //装配
        if (ref == nullptr) return;
        FITKAbaAssembly* ass = ref->getAbstractModelAs<FITKAbaAssembly>();
        if (ass == nullptr) return;
        //集合
        FITKModelSet* set = FITKDATAREPO->getTDataByID<FITKModelSet>(ref->getComponentID());
        if (set == nullptr || set->getModelSetType() != FITKModelEnum::FMSNode) return;
        const QList<int>&  mem = set->getMember();
        if (mem.isEmpty()) return;
        //节点id
        const int id = mem.at(0);
        FITKNodeList* nodes = ass->getPoints();
        nodes->removeNodeByID(id);

        auto ssm = ass->getComponentManager();
        if (ssm == nullptr) return;
        ssm->removeDataObj(set);
    }

}


